package com.zzgc.modules.controller;

import com.zzgc.common.dao.StudentTableDao;
import com.zzgc.common.utils.JWTUtil;
import com.zzgc.modules.async.AsyncStatistical;
import com.zzgc.modules.log.ApiLog;
import com.zzgc.modules.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;

/**
 * 把控制层的公共部分提取出来
 * @author
 * @version 1.0
 * @date 2020/8/5 14:34
 * @description 基础controller
 */
public class BaseController {

    // 异步类
    @Autowired
    AsyncStatistical asyncStatistical;

    // 可视化服务
    @Autowired
    StaticDistrictVisualizationService districtVisualizationService;
    @Autowired
    StaticSchoolVisualizationService schoolVisualizationService;
    @Autowired
    StaticCityVisualizationService cityVisualizationService;
    @Autowired
    StaticProvincialVisualizationService provincialVisualizationService;

    /**
     * 注入全部service
     */
    @Autowired
    AuthUserService userService;
    @Autowired
    AuthRoleService roleService;
    @Autowired
    AuthUserRoleService userRoleService;
    @Autowired
    AuthDepartService departService;
//    @Autowired
//    AuthRightService rightService;
//    @Autowired
//    AuthRoleRightService roleRightService;
    @Autowired
    StudentArchivesService studentArchivesService;
    @Autowired
    SysDictService sysDictService;
    @Autowired
    SysDictItemService sysDictItemService;
    @Autowired
    StudentArchivesService archivesService;
    @Autowired
    StudentTableDao studentTableDao;
    @Autowired
    StudentSchoolService studentSchoolService;
    @Autowired
    StudentService studentService;
    @Autowired
    SchoolService schoolService;
    @Autowired
    SchoolPlanService schoolPlanService;


    /**
     * 获取全局属性
     */
//    @Value("${port.url}")
//    protected String portUrl;
//    @Value("${download.file.path}")
//    protected String downloadFilePath;

    /**
     * 创建session、Request、Response等对象
     */
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;
    /**
     *    payload.put("userName", user.getAName()); // 用户名
     *    payload.put("userId", user.getId() + ""); // 用户id
     *    payload.put("schoolId", user.getASchoolId() == null ? null : user.getASchoolId() + ""); // 学校id
     *    payload.put("aDepartId", user.getADepartId() == null ? null : user.getADepartId() + ""); // 部门id
     *    payload.put("regionId", authDepart.getARegionId() == null ? null : authDepart.getARegionId() + ""); // 区域编号
     *    payload.put("role", authUserRole.getARoleId() == null ? null : authUserRole.getARoleId() + "");// 角色信息
     *    // 角色类型 1：系统角色 2：普通角色
     *    payload.put("type", authRole.getAType() == 1 ? StatelessRealm.ADMIN_AUTHENTICATION : StatelessRealm.USER_AUTHENTICATION);// 用户类型
     *
     * @Date 2022/11/19  17:29
     * @Version 1.0
     */
    protected String token;


    /**
     * 在每个子类方法调用之前先调用
     * 设置request,response,session这三个对象
     *
     * @param request
     * @param response
     */
    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        this.session = request.getSession(true);
        this.token = request.getHeader("token");
        //可以在此处拿到当前登录的用户
    }

    /**
     * 获取学校的表名
     * @ClassName BaseController
     * @Date 2022/11/23  15:19
     * @Version 1.0
     */
    public String getStudentTable() {
        //String schoolId = JWTUtil.resolveClaim(token, "schoolId");
        String regionId = JWTUtil.resolveClaim(token, "regionId");
        if (regionId == null){
            throw new RuntimeException("区域编号为空!");
        }
        // 添加表名
        return "archives_" + regionId;
    }

    /**
     *  获取用户的role
     * @ClassName BaseController
     * @Date 2022/11/23  15:19
     * @Version 1.0
     */
    public String getUserRole(){
        String role = JWTUtil.resolveClaim(token, "role");
        if (role == null) {
            throw new RuntimeException("用户权限为空!");
        }
        return role;
    }

    /**
     * 获取用户名
     * @ClassName BaseController
     * @Date 2022/11/24  15:19
     * @Version 1.0
     */
    public String getUserName(){
        String userName = JWTUtil.resolveClaim(token, "userName");
        if (userName == null) {
            throw new RuntimeException("用户名为空!");
        }
        return userName;
    }

    /**
     *  获取用户所属学校id
     * @ClassName BaseController
     * @Date 2022/11/24  15:20
     * @Version 1.0
     */
    public String getSchoolId(){
        String schoolId = JWTUtil.resolveClaim(token, "schoolId");
        if (schoolId == null) {
            throw new RuntimeException("用户没有所属学校!");
        }
        return schoolId;
    }

    /**
     *  获取用户ID
     * @ClassName BaseController
     * @Date 2022/12/2  10:38
     * @Version 1.0
     */
    public String getUserId(){
        String userId = JWTUtil.resolveClaim(token, "userId");
        if (userId == null){
            throw new RuntimeException("用户ID为空!");
        }
        return userId;
    }

    /**
     *  获取用户部门ID
     * @ClassName BaseController
     * @Date 2022/12/2  10:38
     * @Version 1.0
     */
    public String getDepartId(){
        String departId = JWTUtil.resolveClaim(token, "aDepartId");
        if (departId == null && !getUserRole().equals("1")){
            throw new RuntimeException("用户部门ID为空!");
        }
        return departId;
    }

    // 获取学年
    public static int getsYear() {
        int month = LocalDate.now().getMonth().getValue();
        int year = 0;
        // 如果是8到12月就是本学年, 如果是1到7月就是上一个学年
        if (month > 0 && month <= 7) {
            year = LocalDate.now().getYear() - 1;
        } else {
            year = LocalDate.now().getYear();
        }
        return year;
    }
}
